<template>
  <div>
    <van-nav-bar
      title="收获地址"
      left-arrow
      @click-left="Pages('/addresslist')"
    >
    </van-nav-bar>

    <div>
      <van-form :model="formData">
        <van-field v-model="formData.name" label="姓名" placeholder="姓名" />
        <van-field v-model="formData.tel" label="电话" placeholder="电话" />
        <van-field
          v-model="formData.address"
          label="收获地址"
          placeholder="收货地址"
        />
      </van-form>
      <div style="margin-top: 10px">
        <van-button
          style="margin-top: 10px"
          type="primary"
          round
          size="large"
          @click="onSave"
          v-show="type === 'add'"
          >新增</van-button
        >
        <van-button
          style="margin-top: 10px"
          type="info"
          round
          size="large"
          @click="onEdit"
          v-show="type === 'edit'"
          >修改</van-button
        >
        <van-button
          style="margin-top: 10px"
          type="danger"
          round
          size="large"
          @click="onDelete"
          >删除</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import axios from "axios";
export default {
  data() {
    return {
      type: "",
      searchResult: [],
      addressList: [],
      formData: {
        name: "",
        tel: "",
        address: "",
      },
    };
  },

  mounted() {
    this.type = this.$route.query.type;
    this.getData();
    if (this.$route.query.data) {
      this.formData = this.$route.query.data;
    }
  },
  methods: {
    //列表
    getData() {
      axios({
        url: "http://127.0.0.1:3000/address/list",
        method: "get",
      }).then((res) => {
        this.addressList = res.data;
        console.log("aaa", this.addressList);
      });
    },
    //新增
    onSave() {
      axios({
        url: "http://127.0.0.1:3000/address/add",
        method: "post",
        data: {
          ...this.formData,
        },
      }).then((res) => {
        if (res.data.code === 200) {
          Toast.success("新增成功");
        } else {
          Toast.fail("新增失败");
        }
        this.getData();
      });
      this.$router.go(-1);
    },
    //编辑
    onEdit() {
      axios({
        url: "http://127.0.0.1:3000/address/update",
        method: "get",
        params: {
          ...this.formData,
        },
      }).then((res) => {
        // console.log(res)
        if (res.data.code === 200) {
          Toast.success("编辑成功");
        } else {
          Toast.fail("编辑失败");
        }
        this.getData();
      });
      this.$router.go(-1);
    },
    onDelete() {
      axios({
        url: "http://127.0.0.1:3000/address/delete",
        method: "get",
        params: {
          ...this.formData,
        },
      }).then((res) => {
        if (res.data.code === 200) {
          Toast.success("删除成功");
        } else {
          Toast.fail("删除失败");
        }

        this.getData();
      });
      this.$router.go(-1);
    },

    Pages(path) {
      if (this.$route.path === path) {
        return;
      }
      this.$router.push({
        path: path,
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.header {
  background: #f2f2f2;
  height: 100%;
}
</style>>